package com.leezai.mqtt.utils;


import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

public class ShapefileToGeoTrellisVectorTiles {

    public static void main(String[] args) {
        // 电子栅栏多边形的经纬度坐标列表
        List<Point2D.Double> polygonCoords = new ArrayList<>();
        polygonCoords.add(new Point2D.Double(39.930636, 116.412928));
        polygonCoords.add(new Point2D.Double(39.928658, 116.413198));
        polygonCoords.add(new Point2D.Double(39.928713, 116.417348));
        polygonCoords.add(new Point2D.Double(39.930678, 116.417043));
        polygonCoords.add(new Point2D.Double(39.930636, 116.412928));

        // 点的经纬度坐标列表
        List<Point2D.Double> pointCoords = new ArrayList<>();
        pointCoords.add(new Point2D.Double(39.929792, 116.414797));
        pointCoords.add(new Point2D.Double(39.929798, 116.414797));
        pointCoords.add(new Point2D.Double(37.400, -122.700));

        for (Point2D.Double point : pointCoords) {
            if (isPointInPolygon(point, polygonCoords)) {
                System.out.println("Point (" + point.getX() + ", " + point.getY() + ") is inside the polygon.");
            } else {
                System.out.println("Point (" + point.getX() + ", " + point.getY() + ") is outside the polygon.");
            }
        }
    }

    // 检查点是否位于多边形内
    public static boolean isPointInPolygon(Point2D.Double point, List<Point2D.Double> polygonCoords) {
        Path2D.Double polygon = new Path2D.Double();
        for (int i = 0; i < polygonCoords.size(); i++) {
            Point2D.Double coord = polygonCoords.get(i);
            if (i == 0) {
                polygon.moveTo(coord.getX(), coord.getY());
            } else {
                polygon.lineTo(coord.getX(), coord.getY());
            }
        }
        polygon.closePath();
        return polygon.contains(point);
    }


}
